package com.yugao.fintech.draper.datascope.handler;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.yugao.fintech.draper.datascope.strategy.DataScopeStrategy;

import java.util.List;

/**
 * SQL解析上下文 方便透传 {@link DataScopeStrategy} 规则
 */
public class DataScopeStrategyContextHolder {
	/**
	 * MappedStatement 对应的规则
	 */
	private static final ThreadLocal<List<DataScopeStrategy>> RULES = new TransmittableThreadLocal<>();

	/**
	 * SQL 是否进行重写
	 */
	private static final ThreadLocal<Boolean> REWRITE = new TransmittableThreadLocal<>();

	public static void init(List<DataScopeStrategy> rules) {
		RULES.set(rules);
		REWRITE.set(false);
	}

	public static void clear() {
		RULES.remove();
		REWRITE.remove();
	}

	public static boolean getRewrite() {
		return REWRITE.get();
	}

	public static void setRewrite(boolean rewrite) {
		REWRITE.set(rewrite);
	}

	public static List<DataScopeStrategy> getRules() {
		return RULES.get();
	}

}